// components/task_list/task_list.js
const myBehavior = require("../../behaviors/requests")
Component({
  /**
   * 组件的属性列表
   */
  behaviors: [myBehavior],
  options: {
    styleIsolation: 'shared',
  },
  item: '',
  my_timer: '',
  properties: {
    user_num: {
      type: String,
      value: ''
    },
    shipping_list: {
      type: Array,
      value: []
    },
    factory: {
      type: String,
      value: ''
    },
    material_type: {
      type: Array,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    loading: false,
    finished: false,
    list: [],
    base_urlstr: "https://logisticsengineering.gree.com",
    count_stepper: 1,
    show: false,
    item: null,
    my_timer3: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    dealTask(ev) {
      var item = ev.currentTarget.dataset.item;
      if (item.status == "未接单") {
        this.item = item;
        this.setData({
          show: true
        })
      } else {
        this.completeTask(item)
      }
    },
    onChange(ev) {
      this.setData({
        count_stepper: ev.detail
      })
    },
    acceptTask() {
      var url = "https://logisticsengineering.gree.com/order_system/reverse/deliver"
      var data = {
        _id: this.item._id,
        count: this.data.count_stepper,
        employee_num: this.data.user_num
      }
      this.post(url, data).then(res => {
        if (res.code == 20000) {
          wx.showToast({
            title: res.msg,
            icon: "success"
          })
          this.get_task_doing({
            employee_num: this.properties.user_num,
            shipping_list: this.properties.shipping_list,
            factory: this.properties.factory,
            material_type: this.properties.material_type
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "error"
          })
        }
      }).catch(err => {
        wx.showToast({
          title: err.msg,
          icon: "error"
        })
      })
    },
    completeTask(item) {
      var url = "https://logisticsengineering.gree.com/order_system/reverse/arrive"
      var data = {
        _id: item._id
      }
      this.post(url, data).then(res => {
        if (res.code == 20000) {
          wx.showToast({
            title: res.msg,
            icon: "success"
          });
          this.get_task_doing({
            employee_num: this.properties.user_num,
            shipping_list: this.properties.shipping_list,
            factory: this.properties.factory,
            material_type: this.properties.material_type
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "error"
          })
        }
      }).catch(err => {
        wx.showToast({
          title: err.msg,
          icon: "error"
        })
      })
    },
    get_task_doing(data) {
      let url_str = this.data.base_urlstr + "/order_system/reverse/find_order";
      this.post(url_str, data).then(res => {
        if (res.code == 20000) {
          var data = res.data;
          this.setData({
            list: [...data]
          })
        } else {
          this.setData({
            list: []
          })
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
          clearInterval(this.my_timer)
        }
      }).catch(err => {
        this.setData({
          list: []
        })
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
        clearInterval(this.my_timer)
      })
    },
    restart_task_query() {
      clearInterval(this.my_timer)
      this.get_task_doing({
        employee_num: this.properties.user_num,
      })
      this.my_timer = setInterval(() => {
        if (this.properties.user_num) {
          this.get_task_doing({
            employee_num: this.properties.user_num,
            shipping_list: this.properties.shipping_list,
            factory: this.properties.factory,
            material_type: this.properties.material_type
          });
        }
      }, 3000);
    }
  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      // var employee_num = wx.getStorageSync('delivery_num')
      // if (employee_num) {
      //   var data = {
      //     employee_num: employee_num
      //   }
      //   this.get_task_doing(data)
      // }
    },
    ready: function () {
      //渲染在页面中
      // console.log("ready")
      this.restart_task_query()
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
      // console.log("detached")
      clearInterval(this.my_timer)
    },
  },
})